package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.response.*;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.atguigu.springcloud.constant.BusinessStatus.TOKEN_ERROR;

/**
 * Created by luojie on 2018/9/6.
 */
public class BaseController<T> {


    //--------------- success ----------------

    protected BaseResponse successSaveBaseResponse(){
        return new BaseResponse(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase());
    }

    protected BaseResponse successDeleteBaseResponse(){
        return new BaseResponse(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase());
    }

    protected BaseResponse successUpdateBaseResponse(){
        return new BaseResponse(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase());
    }

    protected BaseResponse successQueryBaseResponse(){
        return new BaseResponse(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase());
    }

    @SuppressWarnings("unchecked")
    protected OneResponse successQueryOneResponse(T t){
        return new OneResponse(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), t);
    }

    @SuppressWarnings("unchecked")
    protected PageResponse successQueryPageResponse(PageResponseDetail pageResponseDetail, List<T> data) {
        return new PageResponse(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pageResponseDetail, data);
    }

    @SuppressWarnings("unchecked")
    protected MultiResponse successQueryMultiResponse(List<T> data) {
        return new MultiResponse(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), data);
    }

    protected BaseResponse successBaseResponse(String desc){
        return new BaseResponse(HttpStatus.OK.value(), desc);
    }

    //--------------- token error ----------------

    protected BaseResponse tokenErrorBaseResponse(HttpServletResponse response) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        return new BaseResponse(TOKEN_ERROR.value(), TOKEN_ERROR.getReasonPhrase());
    }

    //--------------- 500 server error ----------------

    protected BaseResponse serverExceptionBaseResponse(HttpServletResponse response) {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        return new BaseResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
    }

    //--------------- 404 not found ----------------

    protected BaseResponse notFoundBaseResponse(String desc, HttpServletResponse response){
        response.setStatus(HttpStatus.NOT_FOUND.value());
        return new BaseResponse(HttpStatus.NOT_FOUND.value(), desc);
    }

    protected BaseResponse notFoundBaseResponse(Integer status, String desc, HttpServletResponse response){
        response.setStatus(HttpStatus.NOT_FOUND.value());
        return new BaseResponse(status, desc);
    }

    @SuppressWarnings("unchecked")
    protected OneResponse notFoundOneResponse(String desc, HttpServletResponse response){
        response.setStatus(HttpStatus.NOT_FOUND.value());
        return new OneResponse(HttpStatus.NOT_FOUND.value(), desc, null);
    }

    protected OneResponse notFoundOneResponse(String desc,HttpServletResponse response , T t){
        response.setStatus(HttpStatus.NOT_FOUND.value());
        return new OneResponse(HttpStatus.NOT_FOUND.value(), desc, t);
    }
    @SuppressWarnings("unchecked")
    protected OneResponse notFoundOneResponse(Integer status,String desc, HttpServletResponse response){
        response.setStatus(HttpStatus.NOT_FOUND.value());
        return new OneResponse(status, desc, null);
    }

    //--------------- 400 ----------------

    protected BaseResponse invalidRequestBaseResponse(HttpServletResponse response){
        response.setStatus(HttpStatus.BAD_REQUEST.value());
        return new BaseResponse(HttpStatus.BAD_REQUEST.value(), "无效请求");
    }

    //--------------- 401 ----------------

    protected BaseResponse unauthorizedBaseResponse(String desc, HttpServletResponse response) {
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        return new BaseResponse(HttpStatus.UNAUTHORIZED.value(), StringUtils.isEmpty(desc) ? "未授权访问" : desc);
    }



}

